package com.bls.sms.bo.impl;

import java.util.List;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;

import com.bls.sms.bo.interfaces.ClassBO;
import com.bls.sms.dao.interfaces.ClassDAO;
import com.bls.sms.exception.ApplicationException;
import com.bls.sms.wrapper.ClassWrapper;

public class ClassBOImpl extends BaseBO implements ClassBO {
    private ClassDAO classDAO = null;

    public int saveClass(ClassWrapper classWrapper) throws ApplicationException {
        final ClassWrapper wrapper = classWrapper;
        getTransactionTemplate().execute(new TransactionCallback() {
            public Object doInTransaction(TransactionStatus status) {
                try {
                    try {
                        // int maxId = getSubjectDAO().getMaxId() + 1;
                        getClassDAO().saveClass(wrapper);
                    } catch (ApplicationException e) {
                        e.printStackTrace();
                    }
                } finally {
                    status.setRollbackOnly();
                }
                return null;
            }
        });
        return 1;
    }
    public int updateClass(ClassWrapper classWrapper)
            throws ApplicationException {
        final ClassWrapper wrapper = classWrapper;
        getTransactionTemplate().execute(new TransactionCallback() {
            public Object doInTransaction(TransactionStatus status) {
                try {
                    try {
                        getClassDAO().updateClass(wrapper);
                    } catch (ApplicationException e) {
                        e.printStackTrace();
                    }
                } finally {
                    status.setRollbackOnly();
                }
                return null;
            }
        });
        return 1;
    }
    public int deleteClass(ClassWrapper classWrapper)
            throws ApplicationException {
        final ClassWrapper wrapper = classWrapper;
        getTransactionTemplate().execute(new TransactionCallback() {
            public Object doInTransaction(TransactionStatus status) {
                try {
                    try {
                        // int maxId = getSubjectDAO().getMaxId() + 1;
                        getClassDAO().deleteClass(wrapper);
                    } catch (ApplicationException e) {
                        e.printStackTrace();
                    }
                } finally {
                    status.setRollbackOnly();
                }
                return null;
            }
        });
        return 1;
    }
    public List<ClassWrapper> loadClasses() throws ApplicationException {
        return getClassDAO().loadClasses();
    }
    public ClassDAO getClassDAO() {
        return classDAO;
    }
    public void setClassDAO(ClassDAO classDAO) {
        this.classDAO = classDAO;
    }
}
